﻿/*
 * 管理域的doctor的控制器
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WH.NurseSys.Model;
using WH.NurseSys.BLL;

namespace WH.NurseSys.Web.Areas.Admin.Controllers
{
    public class DoctorController : Controller
    {
        //登录展示
        [HttpGet]
        public ActionResult Login()
        {
            if (Session["doctor"] != null)
            {
                Doctor doctor = Session["doctor"] as Doctor;
                if (doctor.Rank != 3)
                {
                    ViewBag.Message = "该帐号没有足够权限，请联系管理员";
                    return View();
                }
                return Redirect("/admin/doctor/list");
            }
            if (HttpContext.Request.Cookies["doctor"] != null)
            {
                HttpCookie cookieDoctor = HttpContext.Request.Cookies["doctor"];
                string username = cookieDoctor.Values["username"];
                string password = cookieDoctor.Values["password"];
                if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
                    return View();
                int code;
                Doctor doctor = new DoctorBLL().Login(username, password, out code);
                if (code == 2)
                {
                    ViewBag.Message = "帐号错误";                    
                }
                else if (code == 3)
                {
                    ViewBag.Message = "密码错误";
                }
                else
                {
                    if (doctor.Rank == 3)
                    {
                        Session["doctor"] = doctor;
                        return Redirect("/admin/doctor/list");
                    }
                    else
                    {
                        ViewBag.Message = "该帐号没有足够权限，请联系管理员";                       
                    }
                }
            }
            return View();
        }

        //登录逻辑处理
        [HttpPost]
        public ActionResult Login(string id)
        {
            string username = HttpContext.Request.Form["username"];
            string password = HttpContext.Request.Form["password"];
            string save = HttpContext.Request.Form["remember"];            
            if (string.IsNullOrWhiteSpace(username))
            {
                ViewBag.Message = "用户名不能为空";
                return View("~/views/doctor/login.cshtml");
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                ViewBag.Message = "密码不能为空";
                return View("~/views/doctor/login.cshtml");
            }
            int code;
            Doctor doctor = new DoctorBLL().Login(username, password, out code);
            if (code == 1)
            {
                Session["doctor"] = doctor;
                if (!string.IsNullOrWhiteSpace(save))
                {
                    HttpCookie cookieDoctor = new HttpCookie("doctor");
                    cookieDoctor.Values["username"] = username;
                    cookieDoctor.Values["password"] = password;
                    cookieDoctor.Expires = DateTime.Now.AddMonths(1);
                    Response.Cookies.Add(cookieDoctor);
                }
                return Redirect("/patient/list");
            }
            else if (code == 2)
            {
                ViewBag.Message = "帐号错误";
                return View("~/views/doctor/login.cshtml");
            }
            else if (code == 3)
            {
                ViewBag.Message = "密码";
                return View("~/views/doctor/login.cshtml");
            }
            else
            {
                ViewBag.Message = "登录异常，请联系管理员";
                return View("~/views/doctor/login.cshtml");
            }
        }

        //登出
        public ActionResult Logout()
        {
            Session["doctor"] = null;
            HttpCookie cookieDoctor = new HttpCookie("doctor");
            cookieDoctor.Expires = DateTime.Now.AddDays(-100);
            Response.Cookies.Add(cookieDoctor);
            return Redirect("/admin/");
        }

        //列出所有的帐号
        public ActionResult List()
        {
            if (Session["doctor"] == null)
            {               
                return Redirect("/admin/doctor/login");
            }
            int pageIndex=1;
            int pageSize=50;
            string query = null;
            if (!string.IsNullOrWhiteSpace(HttpContext.Request.QueryString["pageIndex"]))
            {
                pageIndex = int.Parse(HttpContext.Request.QueryString["pageIndex"]);
            }
            if (!string.IsNullOrWhiteSpace(HttpContext.Request.QueryString["pageSize"]))
            {
                pageSize=int.Parse(HttpContext.Request.QueryString["pageSize"]);
            }
            if (!string.IsNullOrWhiteSpace(HttpContext.Request.Form["nickname"]))
                query = HttpContext.Request.Form["nickname"];
            List<Doctor> doctorList = new DoctorBLL().GetPagedList(pageIndex, pageSize, query);
            return View(doctorList);
        }

        [HttpPost]
        public ActionResult Insert(string id)
        {
            DoctorBLL doctorBLL = new DoctorBLL();
            string username = Request.Form["username"];
            if (doctorBLL.Exist(username))
                return Redirect("/admin/Doctor/List");
            string nickname = Request.Form["nickname"];
            string password = Request.Form["pwd1"];
            string deptIdText = Request.Form["deptId"];
            Doctor doctor = new Doctor();
            doctor.Id = Guid.NewGuid();
            doctor.Username = username;
            doctor.Nickname = nickname;
            doctor.Pwd = password;
            doctor.Rank = 1;
            doctor.AddDate = DateTime.Now;
            doctor.DeptId = Guid.Parse(deptIdText);
            doctorBLL.Insert(doctor);
            return Redirect("/admin/doctor/list/");
        }

        //插入一个新的医生帐号
        [HttpGet]
        public ActionResult Insert()
        {
            List<Dept> deptList = new DeptBLL().GetList();
            ViewBag.DeptList = deptList;
            return View();
        }

        //删除帐号
        public ActionResult Delete(Guid id)
        {
            DoctorBLL doctorBLL = new DoctorBLL();
            doctorBLL.Delete(id);
            return Redirect("/admin/doctor/list");
        }
    }
}
